package com.weijx.dishes.controller;

import com.weijx.dishes.utils.ReturnResult;
import com.weijx.dishes.entity.Product;
import com.weijx.dishes.service.ProductService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * com.weijx.dishes.controller.ProductController
 * 描述：
 * on 2025/10/24-下午4:00
 *
 * @author weijx
 */

@RestController
@RequestMapping("/api/products")
public class ProductController {

    @Resource
    private ProductService productService;

    /**
     * 获取所有餐品
     */
    @GetMapping
    public ReturnResult<List<Product>> getAllProducts() {
        try {
            List<Product> products = productService.getAllProducts();
            return ReturnResult.success("获取餐品成功", products);
        } catch (Exception e) {
            return ReturnResult.error(500, "获取餐品失败: " + e.getMessage(), null);
        }
    }

    /**
     * 根据分类ID获取餐品
     */
    @GetMapping("/category/{categoryId}")
    public ReturnResult<List<Product>> getProductsByCategoryId(@PathVariable String categoryId) {
        try {
            List<Product> products = productService.getProductsByCategoryId(categoryId);
            return ReturnResult.success("获取餐品成功", products);
        } catch (Exception e) {
            return ReturnResult.error(500, "获取餐品失败: " + e.getMessage(), null);
        }
    }

    /**
     * 根据ID获取餐品
     */
    @GetMapping("/{id}")
    public ReturnResult<Product> getProductById(@PathVariable String id) {
        try {
            Product product = productService.getProductById(id);
            if (product != null) {
                return ReturnResult.success("获取餐品成功", product);
            } else {
                return ReturnResult.error(404, "餐品不存在", null);
            }
        } catch (Exception e) {
            return ReturnResult.error(500, "获取餐品失败: " + e.getMessage(), null);
        }
    }
}
